package com.shujia.sql

import org.apache.spark.sql.{DataFrame, SparkSession}

object Demo8schema {
  def main(args: Array[String]): Unit = {

    val spark: SparkSession = SparkSession
      .builder()
      .appName("sql")
      .master("local")
      .config("spark.sql.shuffle.partitions", 1)
      .getOrCreate()


    import org.apache.spark.sql._
    import org.apache.spark.sql.types._

    // 表的列名和列的类型
    val schema: StructType =
      StructType(List(
        StructField("id", StringType),
        StructField("name", StringType),
        StructField("age", IntegerType),
        StructField("gender", StringType),
        StructField("clazz", StringType)

      ))


    val studentDF: DataFrame = spark.read
      .option("sep", ",")
      //.schema("id STRING,name STRING,age INT,gender STRING,clazz STRING")
      .schema(schema)
      .csv("spark/data/students.txt")


    studentDF.show()
  }
}
